/*
 * Phase.h
 *
 *  Created on: 16/10/2011
 *      Author: godinho
 */

#ifndef PHASE_H_
#define PHASE_H_

#include <vector>
#include <string>

namespace localview {

class Link;
class Node;

/**
 * Objects of this class holds information about a traffic light phase.
 *
 * A phase is composed by a set of links (a link is a pair with an incident
 * and a target street) and its status, also the duration of the phase.
 *
 * On traffic modeling context, a phase is the movements allowed on a traffic
 * intersection in a defined period of time.
 *
 * This object also holds the node that is considered the previous node and
 * the one that is considered the next node. It is due to implement green waves.
 * This notions is totally arbitrary.
 */
class Phase {
public:

	/**
	 * Constructor of a phase object
	 */
	Phase();

	/**
	 * Destructor
	 */
	virtual ~Phase();

	/**
	 * Add a new link on position i on this phase.
	 *
	 * Position of each link on this objects is an important information,
	 * since the status of each link (if its red/green/yellow) can be
	 * retrieved by using the index of link.
	 *
	 * @param link to be added to this phase definition
	 * @return postion of link on this phase definition, this value can be used
	 * 		   to set status of link on phase or to consult.
	 */
	int addLink(Link* link, char status);

	/**
	 * Returns the size of link list.
	 *
	 * @return number of links on this phase
	 */
	int getLinkCount();

	Link* getLink(int index);

	/**
	 * Set status of the first n links on this phase.
	 *
	 * Parameters of this methods follow the SUMO definition of traffic lights
	 * phase.
	 *
	 * @param status Array with caracteres that represent status for links on
	 * 			this phase.
	 */
	void setStatus(const char* status);

	void setStatus(int linkIndex, char status);

	/**
	 * Get status of index-th link.
	 *
	 * @param index of link
	 * @return status of n-th link on this phase
	 */
	char getStatus(int index);

	/**
	 * Get phase duration in seconds
	 *
	 * @return duration in seconds of this phase.
	 */
	int getDuration();

	/**
	 * Set phase duration in seconds
	 *
	 * @param d Phase duration in seconds
	 */
	void setDuration(int d);

	/*
	 * Returns all links on this phase
	 */
	std::vector<Link*> getLinks();

	/**
	 * Given a status s, this method will return all links that are currently
	 * on this state.
	 *
	 * @param a character that represents a status (r, G, y)
	 * @return a list of links
	 */
	std::vector<Link*> getLinksOnStatus(char s);

	/**
	 * Return the node that is considered main next node of this phase.
	 */
    Node *getNext() const;

    /**
     * Return the node that is considered main previous node of this phase.
     *
     * Since all links of a Phase has the same source node, the first added
     * link will be used to set the previos node.
     */
    Node *getPrevious() const;

    /**
     * Set the node that is considered main next node of this phase.
     */
    void setNext(Node *next);

    /**
     * Returns true if this phase is a transition phase.
     *
     * Transition phase are those that there is some link on yellow stage.
     */
    bool isTransitionPhase();

private:
    std::vector<Link*> mLinks;
    std::vector<char> mStatus;
    int mDuration;
    Node *mNext;
    Node *mPrevious;
    bool mTrasitionPhase;
};

}

#endif /* PHASE_H_ */
